package com.baosight.sczx.zm.cs.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cs.DataInterface;
import com.baosight.sczx.common.zm.cInterface.cs.DatasourceIhistory;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cs.domain.ZMCSVariable;

/**
 * 变量
 * <p>Title: ServiceZMCS45.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-2 下午12:15:28  严洪浩  Create
 *</pre>
 */
public class ServiceZMCS45 extends ServiceEPBase {

	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCSVariable</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-26下午04:34:34 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		EiInfo outInfo;
		
		String fdParentId = inInfo.getString("fdParentId");
		String isLeaf = inInfo.getString("isLeaf");
		
		//非叶子节点
		if(isLeaf != null && "0".equals(isLeaf)){
			inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
			outInfo = this.query(inInfo);
		}
		
		//叶子节点	
		else {
			StaticTools.setQueryParameters(inInfo, "fdKeyId", fdParentId, true);//添加查询条件
			outInfo = super.query(inInfo, "ZMCSVariable.query", new ZMCSVariable());
		}
		
		outInfo.addBlock(DataInterface.getDataInterfaceBlock("cmes.cs.dataSource"));//数据源
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.units"));//计量单位
		
		return outInfo;
	}
	
	/**
	 * 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS45</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-9上午03:31:06 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdParentId = inInfo.getString("fdParentId");
		String fdParentTable = inInfo.getString("fdParentTable");
		
		StaticTools.setQueryParameters(inInfo, "fdParentId", fdParentId, true);//添加查询条件
		StaticTools.setQueryParameters(inInfo, "fdParentTable", fdParentTable, true);//添加查询条件
		
		EiInfo outInfo = super.query(inInfo, "ZMCSVariable.query", new ZMCSVariable());
		return outInfo;
	}
	
	/**
	 * 新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCSVariable</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-24下午03:42:53 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		//校验名称、简称是否重复
		boolean validate = validate(inInfo, "ZMCSVariable.validate", new HashMap<String, String>());
		
		if(validate) {
			return inInfo;
		}
		else {
			String id = TablePk.getInstance().getNextValue("T_CI_VARIABLE");
			inInfo.getBlock(EiConstant.resultBlock).setCell(0, "fdKeyId", id);//主键
			String dataType = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdInterfaceType");
			EiInfo outinfo = super.insert(inInfo, "ZMCSVariable.insert", "result");
			
			//iHistory形式的数据源
			if(dataType.equals("0")){
				DatasourceIhistory.insert(inInfo);
			}
			
			return outinfo;
		}
	}	

	/**
	 * 修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCSVariable</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-24下午03:42:41 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		//校验名称、简称是否重复
		String id = inInfo.getBlock("result").getCellStr(0, "fdKeyId");
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdKeyId", id);
		
		boolean validate = validate(inInfo, "ZMCSVariable.validate", params);
		if(validate) {
			return inInfo;
		}
		else {
//			String dataType = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdDatasourceType");
//			String IcvKey = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdIcvKeyId");
//			String DbmsKey = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdDbmsKeyId");
			
			EiInfo outInfo = super.update(inInfo, "ZMCSVariable.update");
			String dataType = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdInterfaceType");
			
			//iHistory形式的数据源
			if(dataType.equals("0")){
				DatasourceIhistory.update(inInfo);
			}
			
			return outInfo;
		}
	}

	/**
	 * 删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCSVariable</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-24下午03:43:07 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		//EiInfo _temp = DatasourceIhistory.delete(inInfo);
		EiInfo _temp = super.delete(inInfo, "ZMCSDatasourceIhistory.parentDelete");
		_temp = super.delete(inInfo, "ZMCSVariable.delete");
		
        EiInfo outInfo = query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 更改变量状态为启用
	 * <p>Function: modifyStop</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-15上午11:26:05 张志芳
	 * </p>
	 *
	 */
	public EiInfo modifyStart(EiInfo inInfo)
	{
		EiInfo _temp = super.update(inInfo, "ZMCSVariable.modifyStart");
        EiInfo outInfo = query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 更改变量状态为停用
	 * <p>Function: modifyStop</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-15上午11:26:05 张志芳
	 * </p>
	 *
	 */
	public EiInfo modifyStop(EiInfo inInfo)
	{
		EiInfo _temp = super.update(inInfo, "ZMCSVariable.modifyStop");
        EiInfo outInfo = query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 校验重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-15上午11:25:35 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		String fdName = inInfo.getBlock("result").getCellStr(0, "fdName");
		String fdNo = inInfo.getBlock("result").getCellStr(0, "fdNo");
		String fdParentId = inInfo.getBlock("result").getCellStr(0, "fdParentId");
		String fdType = inInfo.getBlock("result").getCellStr(0, "fdType");
		
		params.put("fdName", fdName);
		params.put("fdNo", fdNo);
		params.put("fdParentId", fdParentId);
		
		List result = dao.query("ZMCSVariable.validate", params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			ZMCSVariable _temp = (ZMCSVariable)result.get(0);
			
			StringBuffer msg = new StringBuffer("提交失败，");
			if(_temp.getFdNo() != null && _temp.getFdNo().equals(fdNo)) {
				msg.append(" 编号 ");
			}
			if(_temp.getFdName() != null && _temp.getFdName().equals(fdName)) {
				msg.append(" 名称 ");
			}
			msg.append("重复！");
			
			inInfo.setMsg(msg.toString());
			return true;
		}
		
		if(null != fdType && !"1".equals(fdType.trim())) {
			params.put("fdType", fdType);
			String fdParentTable = inInfo.getBlock("result").getCellStr(0, "fdParentTable").toUpperCase();
			
			List temp;
			if("T_CI_PRODUCTIONUNITS".equals(fdParentTable)) {
				//生产单元
				params.put("fdProductionUnitsId", fdParentId);
				temp = dao.query("ZMCSVariable.validatePU", params);
			}
			else {
				//变量组
				params.put("fdVariableGroupId", fdParentId);
				temp = dao.query("ZMCSVariable.validateVG", params);
			}
			
			if(temp.size() > 0) {
				inInfo.setStatus(-1);
				if("0".equals(fdType.trim())) {
					inInfo.setMsg("一个生产单元只能配置一个开关量！");
				}
				if("2".equals(fdType.trim())) {
					inInfo.setMsg("一个生产单元只能配置一个累计时间！");
				}
				return true;
			}
		}

		return false;
	}
}
